﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyOA.Model;
using MyOA.BLL;

namespace MyOA.UI.PersonManage
{
    public partial class BranchManage : System.Web.UI.Page
    {
        BranchInfoManager branchInfoManager = new BranchInfoManager();
        DepartInfoManager departInfoManager = new DepartInfoManager();
        string message = string.Empty;      //提示语句
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    dataBind();
                }
            }
        }
        protected void gvBranchInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
                ImageButton imgbtnDelete = (ImageButton)e.Row.FindControl("imgbtnDelete");
                imgbtnDelete.Attributes.Add("onclick", "return confirm('确定要删除吗?')");
            }
        }
        protected void gvBranchInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string command = e.CommandName.ToString();
            int branchId = Convert.ToInt32(e.CommandArgument);
            if (command == "branchUpdate")
            {
                this.pnShowBranchInfo.Visible = true;     //显示添加信息panel
                //this.btnSave.Disabled = true;            // 启用按钮
                //this.btnAdd.Disabled = false;             //禁用按钮
                Session["branchId"] = branchId;
                BranchInfo branchInfo = branchInfoManager.GetBranchInfoByBranchId(branchId);
                this.txtBranchName.Value = branchInfo.BranchName;
                this.txtBranchShortName.Value = branchInfo.BranchShortName;

            }
            if (command == "branchDelete")
            {
                this.pnShowBranchInfo.Visible = false;   //隐藏添加信息panel
                int result = departInfoManager.GetCountDepartInfoByBranchId(branchId);
                if (result == 0)
                {
                    int num = branchInfoManager.DeleteBranchInfoByBranchId(branchId);
                    if (num > 0)
                    {
                        message = "删除成功";
                    }
                    else
                    {
                        message = "删除失败";
                    }
                }
                else
                {
                    message = "存在关联信息，请查看后再删除";
                }
                string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", message);
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
                dataBind();
            }
        }
        //添加机构信息
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            BranchInfo branchInfo = new BranchInfo();
            branchInfo.BranchName = txtBranchName.Value;
            branchInfo.BranchShortName = txtBranchShortName.Value;
            if (branchInfoManager.GetBranchInfoByBranchName(branchInfo.BranchName).BranchName == "")
            {
                int num = branchInfoManager.AddBranchInfo(branchInfo);
                this.pnShowBranchInfo.Visible = false;
                if (num > 0)
                {
                    message = "添加成功";
                }
                else
                {
                    message = "添加失败";
                }
            }
            else
            {
                message = "对不起，该机构已经存在！";
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", message);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            dataBind();

        }
        //修改机构信息
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int branchId = Convert.ToInt32(Session["branchId"]);
            BranchInfo branchInfo = new BranchInfo();
            branchInfo.BranchId = branchId;
            if (txtBranchName.Value == null)
            {
                message = "不能为空";
                return;
            }
            branchInfo.BranchName = txtBranchName.Value;
            branchInfo.BranchShortName = txtBranchShortName.Value;
            int num = branchInfoManager.UpdateBranchInfoByBranchId(branchInfo);
            this.pnShowBranchInfo.Visible = false;
            if (num > 0)
            {
                message = "修改成功";
            }
            else
            {
                message = "修改失败";
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", message);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            dataBind();
        }
        //用AspNetPager实现分页功能
        protected void anpBranchInfo_PageChanged(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {

            this.anpBranchInfo.CurrentPageIndex = e.NewPageIndex;  //改变当前页
            dataBind();    //重新绑定
        }
        //数据绑定方法
        public void dataBind()
        {
            PagedDataSource pgDataSource = new PagedDataSource();
            pgDataSource.DataSource = branchInfoManager.GetAllBranchInfo();
            pgDataSource.AllowPaging = true;
            pgDataSource.CurrentPageIndex = anpBranchInfo.CurrentPageIndex - 1;
            pgDataSource.PageSize = anpBranchInfo.PageSize;
            anpBranchInfo.RecordCount = branchInfoManager.GetAllBranchInfo().Count();
            this.gvBranchInfo.DataSourceID = null;
            this.gvBranchInfo.DataSource = pgDataSource;
            this.gvBranchInfo.DataBind();

        }
        //图片按钮显示添加机构Panel
        protected void imgbtnAddBranchInfo_Click(object sender, ImageClickEventArgs e)
        {
            this.pnShowBranchInfo.Visible = true;
        }
        //添加按钮显示添加机构Panel
        protected void btnAddUser_Click1(object sender, EventArgs e)
        {
            this.pnShowBranchInfo.Visible = true;
            this.txtBranchName.Value = "";
            this.txtBranchShortName.Value = "";
        }

        protected void anpBranchInfo_PageChanged(object sender, EventArgs e)
        {

            dataBind();    //重新绑定
        }
        //返回按钮
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("BranchManage.aspx");
        }
    }
}
